﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Intel_Client_GUI
{
	public class VkViewModel: INotifyPropertyChanged
	{
		public VkViewModel()
		{
			// 在此点下面插入创建对象所需的代码。
			db = Globle.DbWrap;
			Globle.DateTimeData.OnTimeChanged+=new Intel_Client_GUI.DateTimeModel.DateTimeChangeHandler(DateTimeData_OnTimeChanged);
			
		}
		~VkViewModel()
		{
			Globle.DateTimeData.OnTimeChanged-=this.DateTimeData_OnTimeChanged;
		}
		public DataTable DayChartSource
		{
			get
			{
				return dayTable;
			}
			set
			{
				dayTable = value;
				this.NotifyPropertyChanged("DayChartSource");
			}
		}
		public DataTable ProcessChartSource
		{
			get
			{
				return pcTable;
			}
			set
			{
				pcTable = value;
				this.NotifyPropertyChanged("ProcessChartSource");
			}
		}
		
		public Dictionary<string,int> Colors
		{
			get
			{
				if(colors==null)
					colors = CalcColors(Globle.DateTimeData.TimeAndDuration);
				return colors;
			}
			set
			{
				this.colors=value;
				this.NotifyPropertyChanged("Colors");
			}
		}
		
		public void ShowKeyDetail(int KeyCode)
		{
			string SQL = string.Format("select count(1) as cnt, strftime('%Y-%m-%d',timestamp) as date from KeyRecords where key={0} and timetick group by date order by date",KeyCode);
			//System.Windows.MessageBox.Show(SQL);
			DataTable dt = db.GetQueryTable(SQL,Globle.DateTimeData.TimeAndDuration.start,Globle.DateTimeData.TimeAndDuration.duration);
			
			SQL = string.Format("select count(1) as cnt, process from KeyRecords where key={0} and timetick group by process order by cnt desc",KeyCode);
			DataTable dt2=db.GetQueryTable(SQL,Globle.DateTimeData.TimeAndDuration.start,Globle.DateTimeData.TimeAndDuration.duration);
			
			this.DayChartSource=dt;
			this.ProcessChartSource = dt2;
		}
		
		private Dictionary<string,int> CalcColors(DateTimeModel.TimeInfo info)
		{
			DataTable dt = db.GetQueryTable("select key,count(1)  as num from keyrecords where timetick group by key order by num desc",info.start,info.duration);
			var _colors = new Dictionary<string, int>();
				
			int max;
            if (dt.Rows.Count == 0)
                return null;
			if(!int.TryParse(dt.Rows[0]["num"].ToString(),out max))
				return null;
			foreach (DataRow r in dt.Rows)				
			{
				string key = vkTable[int.Parse(r["key"].ToString())];
				if(key==null)
					continue;
				if(key =="")
					continue;
				int num;
				if(!int.TryParse(r["num"].ToString(),out num))
					continue;
				num = num * 100 / max ;
				_colors.Add(key,num);
			}
			return _colors;
		}
		
		private Dictionary<string,int> colors=null;
		
		private DataTable pcTable=null;
		
		private DataTable dayTable =null;
		
		private DataBaseWrap db;
		
		#region INotifyPropertyChanged
		public event PropertyChangedEventHandler PropertyChanged;

		
		private void NotifyPropertyChanged(String info)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(info));
			}
		}
		#endregion
		
		
		
		private string[] vkTable = {
			"", "VK_LBUTTON", "VK_RBUTTON", "VK_CANCEL", "VK_MBUTTON", "VK_XBUTTON1", "VK_XBUTTON2", "", "VK_BACK", "VK_TAB", "", "", "VK_CLEAR", "VK_RETURN", "", "", 
			"VK_SHIFT", "VK_CONTROL", "VK_MENU", "VK_PAUSE", "VK_CAPITAL", "VK_HANGUL", "", "VK_JUNJA", "VK_FINAL", "VK_HANJA", "", "VK_ESCAPE", "VK_CONVERT", "VK_NONCONVERT", "VK_ACCEPT", "VK_MODECHANGE", 
			"VK_SPACE", "VK_PRIOR", "VK_NEXT", "VK_END", "VK_HOME", "VK_LEFT", "VK_UP", "VK_RIGHT", "VK_DOWN", "VK_SELECT", "VK_PRINT", "VK_EXECUTE", "VK_SNAPSHOT", "VK_INSERT", "VK_DELETE", "VK_HELP", 
			"VK0", "VK1", "VK2", "VK3", "VK4", "VK5", "VK6", "VK7", "VK8", "VK9", "", "", "", "", "", "", 
			"", "VKA", "VKB", "VKC", "VKD", "VKE", "VKF", "VKG", "VKH", "VKI", "VKJ", "VKK", "VKL", "VKM", "VKN", "VKO", 
			"VKP", "VKQ", "VKR", "VKS", "VKT", "VKU", "VKV", "VKW", "VKX", "VKY", "VKZ", "VK_LWIN", "VK_RWIN", "VK_APPS", "", "VK_SLEEP", 
			"VK_NUMPAD0", "VK_NUMPAD1", "VK_NUMPAD2", "VK_NUMPAD3", "VK_NUMPAD4", "VK_NUMPAD5", "VK_NUMPAD6", "VK_NUMPAD7", "VK_NUMPAD8", "VK_NUMPAD9", "VK_MULTIPLY", "VK_ADD", "VK_SEPARATOR", "VK_SUBTRACT", "VK_DECIMAL", "VK_DIVIDE", 
			"VK_F1", "VK_F2", "VK_F3", "VK_F4", "VK_F5", "VK_F6", "VK_F7", "VK_F8", "VK_F9", "VK_F10", "VK_F11", "VK_F12", "VK_F13", "VK_F14", "VK_F15", "VK_F16", 
			"VK_F17", "VK_F18", "VK_F19", "VK_F20", "VK_F21", "VK_F22", "VK_F23", "VK_F24", "", "", "", "", "", "", "", "", 
			"VK_NUMLOCK", "VK_SCROLL", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
			"VK_LSHIFT", "VK_RSHIFT", "VK_LCONTROL", "VK_RCONTROL", "VK_LMENU", "VK_RMENU", "VK_BROWSER_BACK", "VK_BROWSER_FORWARD", "VK_BROWSER_REFRESH", "VK_BROWSER_STOP", "VK_BROWSER_SEARCH", "VK_BROWSER_FAVORITES", "VK_BROWSER_HOME", "VK_VOLUME_MUTE", "VK_VOLUME_DOWN", "VK_VOLUME_UP", 
			"VK_MEDIA_NEXT_TRACK", "VK_MEDIA_PREV_TRACK", "VK_MEDIA_STOP", "VK_MEDIA_PLAY_PAUSE", "VK_LAUNCH_MAIL", "VK_LAUNCH_MEDIA_SELECT", "VK_LAUNCH_APP1", "VK_LAUNCH_APP2", "", "", "VK_OEM_1", "VK_OEM_PLUS", "VK_OEM_COMMA", "VK_OEM_MINUS", "VK_OEM_PERIOD", "VK_OEM_2", 
			"VK_OEM_3", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
			"", "", "", "", "", "", "", "", "", "", "", "VK_OEM_4", "VK_OEM_5", "VK_OEM_6", "VK_OEM_7", "VK_OEM_8", 
			"", "", "VK_OEM_102", "", "", "VK_PROCESSKEY", "", "VK_PACKET", "", "", "", "", "", "", "", "", 
			"", "", "", "", "", "", "VK_ATTN", "VK_CRSEL", "VK_EXSEL", "VK_EREOF", "VK_PLAY", "VK_ZOOM", "VK_NONAME", "VK_PA1", "VK_OEM_CLEAR", ""
													};

		private void DateTimeData_OnTimeChanged(Intel_Client_GUI.DateTimeModel.TimeInfo info)
		{
			// 在此处添加事件处理程序实现。
			Colors = this.CalcColors(info);
		}
	}
}